using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Mask;
//using System.Text.RegularExpressions;
using System.Globalization;
using ProgramMain.Framework;

namespace ProgramMain.PhoneDirections
{
    public partial class PhoneDirCodeDetailCtl : BaseDetailCtl
    {
        public BindingSource phoneDirectionDataSource;
        private bool m_bEditMode = false;
        private bool m_bOnActionClose = false;
        public bool m_bEnableChangeType = true;

        public PhoneDirCodeDetailCtl()
        {
            InitializeComponent();
        }

        public DialogResult ShowModal(bool IsEditable)
        {
            this.btnSave.Enabled = IsEditable;

            return frmBaseModal.ShowModal(this, labelCaption.Text, this.btnSave, this.btnCancel);
        }

        override public void FrmCommon_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.FormClosed += new FormClosedEventHandler(this.FrmCommon_FormClosed);
                this.ParentForm.FormClosing += new FormClosingEventHandler(this.FrmCommon_FormClosing);
            }

            DataRowView CurrentRowView = (DataRowView)this.phoneDirectionDataSource.Current;
            m_bEditMode = CurrentRowView.Row.RowState != DataRowState.Added
                && CurrentRowView.Row.RowState != DataRowState.Detached;

            if (!m_bEditMode)
            {
                textEndDate.Visible = false;
                dateEditEndDate.Visible = false;
                btnChooseEndDate.Visible = false;
            } 
            
            this.BindDataControls();
        }

        private void BindDataControls()
        {
            this.txtEditName.DataBindings.Add(new Binding("Text", this.phoneDirectionDataSource, "Code"));
            this.memoEditDesc.DataBindings.Add(new Binding("Text", this.phoneDirectionDataSource, "Description"));
            this.dateEditStartDate.DataBindings.Add(new Binding("DateTime", this.phoneDirectionDataSource, "StartDate", true, DataSourceUpdateMode.OnPropertyChanged, null));
            this.dateEditEndDate.DataBindings.Add(new Binding("DateTime", this.phoneDirectionDataSource, "EndDate", true, DataSourceUpdateMode.OnPropertyChanged, null));
        
            if (!m_bEditMode)
            {
                dateEditStartDate.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                dateEditEndDate.EditValue = new DateTime(2100, 01, 01);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider1.Validate())
            {
                foreach (Control c in dxValidationProvider1.GetInvalidControls())
                {
                    DevExpress.XtraEditors.DXErrorProvider.ValidationRuleBase rule = 
                        dxValidationProvider1.GetValidationRule(c);
                    XtraMessageBox.Show(rule.ErrorText);
                    c.Focus();
                    return;
                }
            }

            m_bOnActionClose = true;

            this.phoneDirectionDataSource.EndEdit();
            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = true;

            if (m_bEditMode)
            {
                this.phoneDirectionDataSource.CancelEdit();
            }
            else
            {
                this.phoneDirectionDataSource.RemoveCurrent();
            }
            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        public void FrmCommon_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!m_bOnActionClose)
            {
                m_bOnActionClose = true;
                if (m_bEditMode)
                {
                    this.phoneDirectionDataSource.CancelEdit();
                }
                else
                {
                    this.phoneDirectionDataSource.RemoveCurrent();
                }
            }
        }

        public void FrmCommon_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.ParentForm != null)
            {
                e.Cancel = this.ParentForm.DialogResult != DialogResult.Cancel 
                    && !dxValidationProvider1.Validate();
            }
        }

        private void btnChooseCurrentDate_Click(object sender, EventArgs e)
        {
            dateEditStartDate.EditValue = DateTime.Now.Date;
        }

        private void btnChooseEndDate_Click(object sender, EventArgs e)
        {
            dateEditEndDate.EditValue = new DateTime(2100, 01, 01);
        }
    }
}
